home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / doc / python-gnome2-desktop / examples / gnomeprint / example_02.py < prev    next >
Encoding:
Python Source  |  2009-03-14  |  2.9 KB  |  110 lines

  1. #! /usr/bin/env python
  2. #/*
  3. # *  example_02.c: sample gnome-print code
  4. # *
  5. # *  This program is free software; you can redistribute it and/or
  6. # *  modify it under the terms of the GNU Library General Public License
  7. # *  as published by the Free Software Foundation; either version 2 of
  8. # *  the License, or (at your option) any later version.
  9. # *
  10. # *  This program is distributed in the hope that it will be useful,
  11. # *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. # *  GNU Library General Public License for more details.
  14. # *
  15. # *  You should have received a copy of the GNU Library General Public
  16. # *  License along with this program; if not, write to the Free Software
  17. # *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. # *
  19. # *  Authors:
  20. # *    Chema Celorio <chema@ximian.com>
  21. #    Python conversion:
  22. #      Gustavo J. A. M. Carneiro <gustavo@users.sf.net>
  23. # *
  24. # *  Copyright (C) 2002 Ximian Inc. and authors
  25. # *
  26. # */
  27.  
  28. #/*
  29. # * See README
  30. # */
  31.  
  32. import pygtk; pygtk.require("2.0")
  33. import gnomeprint
  34. import gtk.gdk
  35. import struct
  36.  
  37.  
  38. NUMBER_OF_PIXELS=256
  39.  
  40. def my_print_image_from_pixbuf(gpc, pixbuf):
  41.     raw_image = pixbuf.get_pixels()
  42.     has_alpha = pixbuf.get_has_alpha()
  43.     rowstride = pixbuf.get_rowstride()
  44.     height    = pixbuf.get_height()
  45.     width     = pixbuf.get_width()
  46.     
  47.     if has_alpha:
  48.     gpc.rgbaimage(raw_image, width, height, rowstride)
  49.     else:
  50.     gpc.rgbimage(raw_image, width, height, rowstride)
  51.  
  52.  
  53. def my_print_image_from_disk(gpc):
  54.     # Load the image into a pixbuf
  55.     pixbuf = gtk.gdk.pixbuf_new_from_file("sample-image.png")
  56.     # Save the graphic context, scale, print the image and restore
  57.     gpc.gsave()
  58.     gpc.scale(144, 144)
  59.     my_print_image_from_pixbuf(gpc, pixbuf)
  60.     gpc.grestore()
  61.  
  62.  
  63. def my_print_image_from_memory(gpc):
  64.     pixels = NUMBER_OF_PIXELS;
  65.  
  66.     # Create the image in memory
  67.     color_image = []
  68.     for y in xrange(pixels):
  69.     for x in  xrange(pixels):
  70.         color_image.append(struct.pack("BBB",
  71.                        (x + y) >> 1,
  72.                        (x + (pixels - 1  - y)) >> 1,
  73.                        ((pixels - 1 - x) + y) >> 1))
  74.     # All images in postscript are printed on a 1 x 1 square, since we
  75.     # want an image which has a size of 2" by 2" inches, we have to scale
  76.     # the CTM (Current Transformation Matrix). Save the graphic state and
  77.     # restore it after we are done so that our scaling does not affect the
  78.     # drawing calls that follow.
  79.     gpc.gsave()
  80.     gpc.scale(144, 144)
  81.     gpc.rgbimage("".join(color_image), pixels, pixels, pixels * 3)
  82.     gpc.grestore()
  83.  
  84.  
  85. def my_draw(gpc):
  86.     gpc.beginpage("1")
  87.  
  88.     gpc.translate(200, 100)
  89.     my_print_image_from_memory(gpc)
  90.  
  91.     gpc.translate(0, 150)
  92.     my_print_image_from_disk(gpc)
  93.     
  94.     gpc.showpage()
  95.  
  96.  
  97. def my_print():
  98.     job = gnomeprint.Job(gnomeprint.config_default())
  99.     gpc = job.get_context()
  100.  
  101.     my_draw(gpc)
  102.  
  103.     job.close()
  104.     job.print_()
  105.  
  106.  
  107. my_print()
  108. print "Done..."
  109.  
  110.